// import {shareGoods} from "@src/mall/api/good";
import {getShareCode} from "@src/api/catering"

Component({
  properties: {
    goods: {
      type: Object
    },
    storeId: {
      type: Number
    },
  },
  options: {
    addGlobalClass: true
  },
  data: {
    sysWidth: (wx.getSystemInfoSync().windowWidth) - 50, //屏幕宽度 ---悬浮窗位置
    sysHeight: (wx.getSystemInfoSync().windowHeight) / 2 + 60, //屏幕高度
    dialogShow: false,
    qrcode: null,
    qrcodeReq: {},
    postImg: null,
    showImg: false,
    qrcodeSkuId: null,
    shareObj: null,
    postersPicture: ""
  },
  observers: {
    'goods, storeId': function (goods, storeId) {
      if (goods && storeId) {
        setTimeout(() => {
          this.initPoster();
        })
      }
    },
  },
  methods: {

    handleDialogShow() {
      this.setData({dialogShow: true})
    },
    handleDialogClose() {
      this.setData({dialogShow: false,qrcode:null})
    },
    handlePoster(e) {
      this.setData({postImg: e.detail})
    },
     initPoster() {
      if (this.data.storeId && this.data.goods.id) {
        this.init();
        this.fetchShareGoodsQrcode();
      }
    },
    handleToSavePoster() {
      if (!this.data.postImg) {
        this.handleDialogClose()
        wx.showToast({title: "海报生成中,请稍后再试~", icon: "none", duration: 1500})
      } else {
        this.handleDialogClose()
        this.setData({showImg: true})
      }
    },
    stopMove() {
      return true;
    },
    handleCloseSaveImg() {
      this.setData({showImg: false})
    },
    //保存到相册
    handleSavePost() {
      wx.saveImageToPhotosAlbum({
        filePath: this.data.postImg,
        success: () => {
          wx.showToast({title: `海报已保存到系统相册`, icon: "none", duration: 3000});
          this.handleCloseSaveImg();
        },
        fail: err => {
          if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied"
              || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
            // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
            wx.showModal({
              title: "提示",
              content: "需要您授权保存相册",
              showCancel: false,
              success: () => {
                wx.openSetting({
                  success(settingdata) {
                    if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                      this.handleSavePost()
                    } else {
                      wx.showModal({title: "提示", content: "获取保存到相册权限失败，将无法保存到相册", showCancel: false});
                    }
                  }
                });
              }
            });
          }
        }
      });
    },

    init() {
      let {imageUrl,name,specList,price,shareText} =this.data.goods
      let shareObj = {
        imageUrl,
        title:name,
        price:specList.length>0?specList[0].price:price,
        originPrice: specList.length>0?specList[0].price:price,
        shareText
      }
      let oldShareObj = this.data.shareObj;
      if (oldShareObj != null && JSON.stringify(oldShareObj) === JSON.stringify(shareObj)) {
        // 配置一样不更新
        return;
      }
      this.setData({shareObj: shareObj})
    },

    fetchShareGoodsQrcode() {
      let req = {
        itemId: this.data.goods.id,
        shopId: this.data.storeId
      }
      if (this.data.qrcode &&this.data.qrcodeReq.shopId === req.shopId &&this.data.qrcodeReq.itemId === req.itemId) {
        // 参数一样, 不再请求
        return;
      }
      getShareCode(req).then(res => {
        if (res.code === 0) {
          this.setData({qrcode: res.data, qrcodeReq: req})
        }
      })
    },
  }
})
